package com.itboye.jinpuyiyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.home.OrderDetailActivity;
import com.itboye.jinpuyiyuan.activity.me.PaymentRecordActivity;
import com.itboye.jinpuyiyuan.activity.pay.PayActivity;
import com.itboye.jinpuyiyuan.bean.OrderBean;
import com.itboye.jinpuyiyuan.utils.commonadapter.MutiplyCommonAdapter;
import com.itboye.jinpuyiyuan.utils.commonadapter.ViewHolder;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 缴费记录
 */
public class PaymentRecordAdapter extends MutiplyCommonAdapter<OrderBean> {
    List<OrderBean> datas;
    String orderType;

    public PaymentRecordAdapter(Context context, String orderType,List<OrderBean> datas, int... layoutId) {
        super(context, datas, layoutId);
        this.datas=datas;
        this.orderType=orderType;

    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public void getItemPosition(int position) {

    }

    public void addData(List<OrderBean> datas,String orderType){
        this.datas=datas;
        this.orderType=orderType;
        this.notifyDataSetChanged();
    }
    @Override
    public void convert(ViewHolder holder, OrderBean t, int type, final int position) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        if (orderType.equals("1")){//门诊挂号

            holder.setVisible(R.id.payStatusTv,true);
            if (datas.get(position).getIs_am().trim().equals("1")){ //上午
                holder.setText(R.id.medica_content,"挂号:  "+datas.get(position).getReg_date()+"  "+"上午");
            }else if (datas.get(position).getIs_am().trim().equals("2")){ //下午
                holder.setText(R.id.medica_content,"挂号:  "+datas.get(position).getReg_date()+"  "+"下午");
            }else { //晚上
                holder.setText(R.id.medica_content,"挂号:  "+datas.get(position).getReg_date()+"  "+"夜晚");
            }
            Date date = new Date(Long.parseLong(datas.get(position).getCreate_time())*1000);

            String format1 = format.format(date);
            holder.setText(R.id.medica_time,format1);
            holder.setText(R.id.medica_department,"科室:  "+datas.get(position).getDept_name());
            DecimalFormat df = new DecimalFormat("#0.00");
            holder.setText(R.id.medica_money,"费用:  "+df.format(Double.parseDouble(datas.get(position).getPrice())/100));

            if (datas.get(position).getPay_status().equals("0")){ //未付款
                holder.setText(R.id.payStatusTv,"未付款");
                holder.setTextColor(R.id.payStatusTv,mContext.getResources().getColor(R.color.red));
                holder.getView(R.id.payStatusTv).setEnabled(true);
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemredbg));
            }else if (datas.get(position).getPay_status().equals("1")){ //已付款
                holder.setText(R.id.payStatusTv,"已付款");
                holder.setTextColor(R.id.payStatusTv,mContext.getResources().getColor(R.color.black));
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemgraybg));
            }else if (datas.get(position).getPay_status().equals("2")){ //已退款
                holder.setText(R.id.payStatusTv,"已退款");

                holder.setTextColor(R.id.payStatusTv,mContext.getResources().getColor(R.color.work_address));
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemgraybg));

            }else if (datas.get(position).getPay_status().equals("3")){ //退款中
                holder.setText(R.id.payStatusTv,"退款中");
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemgraybg));
            }


        }else if (orderType.equals("3")){ //体检

            holder.setVisible(R.id.payStatusTv,true);
            holder.setText(R.id.medica_content,"体检:"+datas.get(position).getDept_name());
            Date date = new Date(Long.parseLong(datas.get(position).getCreate_time())*1000);

            String format1 = format.format(date);
            holder.setText(R.id.medica_time,format1);
            if (datas.get(position).getIs_menu().equals("1")){
                holder.setText(R.id.medica_department,"体检套餐类型:  自选体检套餐");
            }else {
                holder.setText(R.id.medica_department,"体检套餐类型:  固定体检套餐");
            }

            DecimalFormat df = new DecimalFormat("#0.00");
            holder.setText(R.id.medica_money,"费用:  "+df.format(Double.parseDouble(datas.get(position).getPrice())/100));

            if (datas.get(position).getPay_status().equals("0")){ //未付款
                holder.setText(R.id.payStatusTv,"未付款");
                holder.setTextColor(R.id.payStatusTv,mContext.getResources().getColor(R.color.red));
                holder.getView(R.id.payStatusTv).setEnabled(true);
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemredbg));
            }else if (datas.get(position).getPay_status().equals("1")){ //已付款
                holder.setText(R.id.payStatusTv,"已付款");
                holder.setTextColor(R.id.payStatusTv,mContext.getResources().getColor(R.color.black));
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemgraybg));
            }else if (datas.get(position).getPay_status().equals("2")){ //已退款
                holder.setText(R.id.payStatusTv,"已退款");

                holder.setTextColor(R.id.payStatusTv,mContext.getResources().getColor(R.color.work_address));
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemgraybg));

            }else if (datas.get(position).getPay_status().equals("3")){ //退款中
                holder.setText(R.id.payStatusTv,"退款中");
                holder.getView(R.id.payStatusTv).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shap_payorder_itemgraybg));
            }

        }else{ //住院
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date parse = format1.parse(datas.get(position).getTrade_time());
                String format2 = format.format(parse);
                holder.setText(R.id.medica_time,format2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            holder.setText(R.id.medica_content,"住院预交金:  "+datas.get(position).getTrade_time());
            holder.setVisible(R.id.payStatusTv,false);
            if (datas.get(position).getPay_type().equals("6")){
                holder.setText(R.id.medica_department,"支付方式:  医院窗口支付");
            }else {
                holder.setText(R.id.medica_department,"支付方式:  app支付");
            }
            DecimalFormat df = new DecimalFormat("#0.00");
            holder.setText(R.id.medica_money,"费用:  "+df.format(Double.parseDouble(datas.get(position).getPay_fee())));

        }


        holder.getView(R.id.payStatusTv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.get(position).getPay_status().equals("0")) { //未付款
                    Intent intent = new Intent(mContext, PayActivity.class);
                    intent.putExtra("order_id",datas.get(position).getOrder_code());
                    intent.putExtra("remark","");
                    mContext.startActivity(intent);
                }else {
                    Intent intent = new Intent(mContext, OrderDetailActivity.class);
                    intent.putExtra("order_id",datas.get(position).getOrder_code());
                    mContext.startActivity(intent);
                }
            }
        });
    }



}
